package com.example.myapp.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapp.R;
import com.example.myapp.api.Api;
import com.example.myapp.api.ApiConfig;
import com.example.myapp.api.ResponseCallback;
import com.example.myapp.utils.StringUtils;
import com.example.myapp.vo.LoginResult;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {
    private EditText etAccount;
    private EditText etPwd;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        btnLogin=findViewById(R.id.btnLogin);
        etAccount=findViewById(R.id.account);
        etPwd=findViewById(R.id.pwd);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account=etAccount.getText().toString().trim();
                String pwd=etPwd.getText().toString().trim();
                login(account,pwd);

            }
        });
    }

    private void login(String account, String pwd) {
        if(StringUtils.IsEmptyAndNull(account)){
            showToast("请输入账号");
           // Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
            return;
        }
        if(StringUtils.IsEmptyAndNull(pwd)){
           // Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            showToast("请输入密码");
            return;
        }

        Map<String,Object> m=new HashMap<String,Object> ();
        m.put("email",account);
        m.put("password",pwd);
        Api.config(ApiConfig.LOGIN,m).postRequest(new ResponseCallback() {
            @Override
            public void OnSuccess(String res) {

                Gson gson=new Gson();
               LoginResult loginResult=gson.fromJson(res,LoginResult.class);


               // Log.e("LoginResult",loginResult.getMsg());
                if(loginResult.getCode()==1){
                    String token=loginResult.getUser().getToken();
                    saveStringToSp("token",token);
                    showToastSync(loginResult.getMsg());
                }else{
                    showToastSync(loginResult.getMsg());
                }
            }

            @Override
            public void OnFailure(Exception e) {
                Log.e("onFailure",e.getMessage());
            }
        });

    }

//    private void login(String account, String pwd) {
//        if(StringUtils.IsEmptyAndNull(account)){
//            showToast("请输入账号");
//           // Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
//            return;
//        }
//        if(StringUtils.IsEmptyAndNull(pwd)){
//           // Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
//            showToast("请输入密码");
//            return;
//        }
//
//        OkHttpClient client=new OkHttpClient.Builder().build();
//        Map m=new HashMap();
//        m.put("email",account);
//        m.put("password",pwd);
//        JSONObject jsonObject=new JSONObject(m);
//        String jsonStr=jsonObject.toString();
//        RequestBody requestBody=
//                RequestBody.create(MediaType.parse("application/json;charset=utf-8"),jsonStr);
//        Request request=new Request.Builder()
//                .url(ApiConfig.BASE_URL+"api/user/login")
//                .addHeader("contentType","application/json;charset=utf-8")
//                .post(requestBody)
//                .build();
//        final Call call=client.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//               Log.e("onFailure",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String result=response.body().string();
//
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        showToast(result);
//                    }
//                });
//            }
//        });
//
//    }
}